﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;

namespace NUMIMAX
{
    public class FileUploader
    {
        public static void UploadFiles(string _FileName, string _UploadPath, string _FTPUser, string _FTPPass)
        {
            System.IO.FileInfo _FileInfo = new System.IO.FileInfo(_FileName);
            System.Net.FtpWebRequest _FtpWebRequest = (System.Net.FtpWebRequest)System.Net.FtpWebRequest.Create(new Uri(_UploadPath));
            _FtpWebRequest.Credentials = new System.Net.NetworkCredential(_FTPUser, _FTPPass);
            _FtpWebRequest.KeepAlive = false;
            _FtpWebRequest.Timeout = 20000;
            _FtpWebRequest.Method = System.Net.WebRequestMethods.Ftp.UploadFile;
            _FtpWebRequest.UseBinary = true;
            _FtpWebRequest.ContentLength = _FileInfo.Length;
            int buffLength = 2048;
            byte[] buff = new byte[buffLength];
            System.IO.FileStream _FileStream = _FileInfo.OpenRead();
            try
            {
                System.IO.Stream _Stream = _FtpWebRequest.GetRequestStream();
                int contentLen = _FileStream.Read(buff, 0, buffLength);
                
                while (contentLen != 0)
                {
                    _Stream.Write(buff, 0, contentLen);
                    contentLen = _FileStream.Read(buff, 0, buffLength);
                }

                _Stream.Close();
                _Stream.Dispose();
                _FileStream.Close();
                _FileStream.Dispose();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Błąd", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }
    }
}
